<template>
    <div class="flex justify-between flex-wrap mb-2">
      <el-space wrap :size="10" class="">
        <el-input
          v-model="params.user_name"
          style="width: 150px"
          placeholder="用户名称"
        />
         <el-input
          v-model="params.promo_name"
          style="width: 150px"
          placeholder="优惠码名称"
        />
         <el-input
          v-model="params.deduct_amount"
          style="width: 150px"
          placeholder="可抵扣金额"
        />
         <el-select
        v-model="params.status_id"
        placeholder="状态"
        style="width: 150px"
      >
        <el-option
          v-for="item in status"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
       <el-date-picker
        v-model="params.useage_time"
        type="date"
        placeholder="使用时间"
      />
         <el-date-picker
          v-model="myDate"
          type="daterange"
          range-separator="To"
          start-placeholder="開始日期"
          end-placeholder="結束日期"
        />
        <el-button :icon="Search" plain @click="search">查詢</el-button>
      </el-space>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="user_id" label="ID" />
      <el-table-column prop="user_name" label="用户名称" />
      <el-table-column prop="promo_name" label="优惠码名称" />
      <el-table-column prop="deduct_amount" label="可折抵金额" />
      <el-table-column prop="min_amount" label="最低订单金额" />
      <el-table-column prop="start_time" label="开始时间" />
      <el-table-column prop="end_time" label="结束时间" />
      <el-table-column prop="status" label="状态" />
       <el-table-column prop="useage_time" label="使用时间" />
      <el-table-column prop="remarks" label="备注" />
      <el-table-column fixed="right" label="操作" width="80">
          <template #default="scope">
             <el-button link size="small" type="danger" @click="handleEdit(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
    </el-table>
    <div v-if="total > params.page_size" class="mt-2 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :page-sizes="[10, 20, 30, 40]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next"
        :total="Number(total)"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { Search, PriceTag,ArrowDown } from "@element-plus/icons-vue";
import { $getCodeDetail,$getCodeStatus } from "@api/market";
import { dayjs } from "element-plus";
let params = ref({
  current_page: 1,
  page_size: 10,
  start_time: "",
  end_time: "",
  useage_time:"",
  user_name: "",
  promo_name:"",
  status_id:"",
  deduct_amount:"",
});
const status = ref([]);
const total = ref(0);
const tableData = ref([]);
const myDate = ref([]);
onMounted(() => {
  getFilter();
});
const getFilter = async () => {
  const status_res = await $getCodeStatus();
   status.value = status_res.data.list;
  getData();
};
const getData = async () => {
    if(myDate.value.length!=0){
  params.value.start_time = dayjs(myDate.value[0]).format("YYYY-MM-DD");
  params.value.end_time = dayjs(myDate.value[1]).format("YYYY-MM-DD");
  }
  let res = await $getCodeDetail(params.value);
  if (res.code == 0) {
    tableData.value = res.data.table;
    total.value = res.data.total;
  }
};
const handleSizeChange = (val: number) => {
  params.value.page_size = val;
  getData();
};
const handleCurrentChange = (val: number) => {
  params.value.current_page = val;
  getData();
  console.log(`current page: ${val}`);
};
const handleSelectionChange = (val: array) => {
  console.log(val);
  multipleSelection.value = val;
};
const search = () => {
  console.log(params.value);
  getData();
};
const handleEdit = (index: number, row: any) => {
  console.log(val);
};
</script>

<style lang="scss" scoped></style>
@/api/live
